# -*- coding:utf-8 -*-
#获取糗事百科页面

import urllib
import urllib.request
import re

page = 1
url = 'http://www.qiushibaike.com/hot/page/' + str(7)
user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
headers = { 'User-Agent' : user_agent }
try:
    # request = urllib.request.Request(url,headers = headers)
    # response = urllib.request.urlopen(request)
    request=urllib.request.Request(url,headers=headers)
    response=urllib.request.urlopen(request)



    content = response.read().decode('utf-8')
    print (content)
    fo=open("E:/file.txt","w")
    fo.write(content)
    fo.close();
except urllib.request.URLError as e:
    if hasattr(e,"code"):
        print (e.code)
    if hasattr(e,"reason"):
        print (e.reason)